package com.eseo.munchkinarbiter;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class SpinBox {
	
	public static void configureAsSpinbox(Button minusButton, TextView valueText, Button plusButton) {
		configureAsSpinbox(minusButton, valueText, plusButton, null, null);
	}

	public static void configureAsSpinbox(final Button minusButton, final TextView valueText, final Button plusButton, final Integer min, final Integer max) {
		valueText.setText("0");
		
		minusButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int currentValue;
				try {
					currentValue = Integer.parseInt(valueText.getText().toString()) - 1;
				} catch(NumberFormatException e) {
					currentValue = 0;
				}
				if(min != null && currentValue < min.intValue())
					currentValue = min.intValue();
				valueText.setText(Integer.toString(currentValue));
			}
		});
		
		plusButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int currentValue;
				try {
					currentValue = Integer.parseInt(valueText.getText().toString()) + 1;
				} catch(NumberFormatException e) {
					currentValue = 0;
				}
				if(max != null && currentValue > max.intValue())
					currentValue = max.intValue();
				valueText.setText(Integer.toString(currentValue));
			}
		});
	}
}
